package com.example.module.custom;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Vibrator;
import android.text.TextUtils;

import com.nand.library.base.base.BaseApp;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 模块描述：主控制器
 */
public class CustomNewApp extends BaseApp {
    /**
     * 创建本身
     */
    private static CustomNewApp sInstance;

    /**
     * 用于存放所有启动的Activity的集合
     */
    private List<Activity> oList;
    public Vibrator mVibrator;

    //获取正在运行的activity
    public Activity runningActivity;
    private static Context context;
    public void setRunningActivity(Activity runningActivity) {
        this.runningActivity = runningActivity;
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        sInstance = this;
        /*清空所有的loger打印信息*/
        //创建运行着的activity集合
        oList = new ArrayList<>();
        initModuleApp(this);
        initModuleData(this);
    }

    @Override
    public void initModuleApp(Application application) {

    }

    @Override
    public void initModuleData(Application application) {

    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
    public static Context getContext() {
        return context;
    }
    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 得到本身
     */
    public static CustomNewApp getInstance() {
        return sInstance;
    }


    /**
     * 添加单个Activity
     *
     * @param activity
     */
    public void addActivity_(Activity activity) {
       // Log.i(TAG, "addActivity_: ------"+activity_imessage);
        // 判断当前集合中不存在该Activity
        if (!oList.contains(activity)) {
            oList.add(activity);//把当前Activity添加到集合中
        }
    }

    /**
     * 销毁单个Activity
     *
     * @param activity
     */
    public void removeActivity_(Activity activity) {
        if (oList.contains(activity)) {
            oList.remove(activity);//从集合中移除
            activity.finish();//销毁当前Activity
        }
    }

    /**
     * 销毁所有的Activity
     */
    public void removeAllActivity_() {
        //通过循环，把集合中的所有Activity销毁
        for (Activity activity : oList) {
            activity.finish();
        }
        oList.clear();
    }

    /**
     * 销毁所有的除开MainActivity的Activity
     */
    public void removeAllExceptMainActivity() {
        Activity a = null;
        for (Activity activity : oList) {
            if (activity instanceof MainActivity) {
                a = activity;
            } else {
                activity.finish();
            }
        }
        oList.clear();
        oList.add(a);
    }


    public Activity getRunningActivity() {
        return runningActivity;
    }
}
